package com.jieshuibao.service;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import com.jieshuibao.util.StringUtils;

/**
 * @author :liuxb@jieshuibao.com
 * @version ：2018年8月21日 上午9:32:29
 * 类说明 中文简单的分词器
 */
@Service
public class IKAnalyzerService {
	
	public  static List<String> segment(String text) throws IOException {
        List<String> list = new ArrayList<>();
        list.add(text);
        StringReader re = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(re, true);
        Lexeme lex;
        while ((lex = ik.next()) != null) {
            list.add(lex.getLexemeText());
        }
        return list;
    }
	/**
	 * 去除单个字符 并把相邻单个字符 拼接
	 * @param text
	 * @return
	 * @throws IOException
	 */
	public  static List<String> segmentRemoveSingle(String text) throws IOException {
        List<String> list = new ArrayList<>();
        list.add(text);
        StringReader re = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(re, true);
        Lexeme lex;
        String str="";
        while ((lex = ik.next()) != null) {
        	if (lex.getLexemeText().length()<=1) {
				
        		str=str+lex.getLexemeText();
			}else {
				if (StringUtils.isNotBlank(str)&&str.length()>1) {
					list.add(str);
				}
				str="";
				list.add(lex.getLexemeText());
			}
        	
            
        }
        if (StringUtils.isNotBlank(str)&&str.length()>1) {
        	list.add(str);
			
		}
        return list;
    }

}
